<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<?php session_start(); 
include 'includes/bandeau_sup.php';
include "../User_access/access_admin.php";
?>
<script type="text/javascript">
	function afficher() 
	{ 
		document.getElementById('oui').setAttribute('hidden', 'hidden');
		if(document.getElementById('create-user').statut.value == '2'){
			//document.getElementById('non').setAttribute = 'visible';  
			document.getElementById('oui').removeAttribute('hidden');
		}
	}
</script>
<html>
	<head>
		<title>Compétences | Création de compte</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel='stylesheet' href='includes/style.css' />
		<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700' rel='stylesheet' type='text/css'>
		<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'>
	</head>
	<body>
	
	<header>
			<h1>Création d'un nouvel utilisateur</h1>
	</header>
	<div id='bloc_page'>
		<section>
			<article style="text-align:center;">
				<table style="margin:auto;">
					<form method="post" action="creation_utilisateur.php" id="create-user">
					<tr>
						<td class="name">Nom &rarr;</td>
						<td><input type="text" name="name" value="" id="name" required /></td>
					</tr>
					<tr>
						<td class="name">Prénom &rarr;</td>
						<td><input type="text" name="prenom" value="" id="prenom" required/></td>
					</tr>
					<tr>
						<td class="name">Adresse mail &rarr;</td>
						<td><input type="text" name="mail" value="" id="mail" required/></td>
					</tr>
					<tr>
						<td class="name">Statut &rarr;</td>
						<td><select name="statut" id="statut" onChange="afficher();" required>
								<option id="enseignant" value="1">Enseignant</option>
								<option id="eleve" value="2" selected>Eleve</option>
						</select></td>
					</tr>
					<tr id="oui">
						<td class="name">Promo &rarr;</td>
						<td><select name="promo" id="oui" required>
								<option value="NULL" selected>Veuillez choisir ...</option>
								<option value="1">SISR</option>
								<option value="2">SLAM</option>
							</select></td>
					</tr>
					<tr>
						<td class="name">Nom d'utilisateur &rarr;</td>
						<td><input type="text" name="user" value="" id="user" required /></td>
					</tr>
					<tr>
						<td class="name">Mot de passe &rarr;</td>
						<td><input type="password" name="mdp" value="" id="mdp" required /></td>
					</tr>
					<tr>
						<td colspan=2 class="titre"><input type="submit" name="creer" value="Creer">
						<input type="reset" value ="Annuler"></td>
					</tr>
					</form>	
				</table>

				<p><a href="index.php">Revenir au menu</a></p>	
			</article>
			<article>
				<p>
						<?php

							include "../conf/config.php";
							if (isset($_POST['creer']))	{
								$db_name="website"; // Database name 
								$nom=$_POST['name'];
								$prenom=utf8_encode($_POST['prenom']);
								$statut=utf8_encode($_POST['statut']);
								$promo=$_POST['promo'];
								$mdp=md5($_POST['mdp']);
								$login=$_POST['user'];
								$mail=utf8_decode($_POST['mail']);
							
							
							
								// Connect to server and select databse.
								mysql_connect($host,$granted_user,$granted_mdp)or die("impossible de se connecter"); 
								mysql_select_db("$db_name")or die("ne peut selectioner DB");
								
							
								if ($statut == 1) 
									{
										$sql="INSERT INTO user(Prenom, Nom, FK_statut, login, mypassword, email)
												VALUES ('$prenom','$nom','$statut', '$login', '$mdp', '$mail');";
										//echo $sql;
										$valide = mysql_query($sql); 
									}
								else
									{
										$sql="INSERT INTO user(Prenom, Nom, FK_statut, FK_eleve, login, mypassword, email)
												VALUES ('$prenom','$nom','$statut', '$promo', '$login', '$mdp', '$mail');";
										// echo $sql;
										$query=mysql_query($sql); 
										
										$id = mysql_insert_id();
										/*echo $id;*/
										
										/* 
										Peuple la table EVO à 0%
										
										NE PAS MODIFIER CETTE PARTIE ELLE EST FONCTIONNELLE
										
										*/
										$sql = "SELECT id_comp FROM comp WHERE FK_promo = '$promo' OR FK_promo = 3 ;";
										$query=mysql_query($sql) or die('!!!!!');
										
										while($line = mysql_fetch_array($query)) //PLACE TOUS LES ID DE COMP DANS UNE TABLE
										{
											// INITIALISE POUR CHAQUE UTILISATEUR CREE, DANS LA TABLE EVO, LA COMP $line à 0 A LA DATE D'AUJOURD'HUI
											$sql = "INSERT INTO evo(id_comp, id_user, progression, date_modif)
													VALUES ($line[0], $id, 0, NOW());";
											//echo $sql."<br />";	
											$valide = mysql_query($sql);
										}
												
									}
									
									if ($valide == 1) {echo "Compte correctement créé.";}
									else {echo "Veuillez choisir un autre nom d'utilisateur.";}
								}
						?>
					
							
						</p>
			</article>
		</section>
	<?php include "/includes/footer.php" ?>
	</div>
	</body>
</html>	